package com.easyweb.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.easyweb.cache.StartUpListener;
import com.easyweb.common.CommonUtils;
import com.easyweb.common.PrintUtils;
import com.easyweb.model.Article;
import com.easyweb.model.PollingPicture;
import com.easyweb.service.ArticleService;
import com.easyweb.service.PollingPictureService;

@Controller
@RequestMapping("/index")
public class IndexController {
	
	@Autowired
	private ArticleService articleService;
	@Autowired
	private PollingPictureService pollingPictureService;
	
	@RequestMapping("/about")
	public String about(HttpServletRequest request){
		fillHeaderAndFooter(request);
		return "front/about";
	}
	
	@RequestMapping("/main")
	public String index(HttpServletRequest request){
		fillHeaderAndFooter(request);
		return "front/index";
	}
	
	@RequestMapping(value="/nav/*",method=RequestMethod.GET)
	public String nav(HttpServletRequest request){
		String url = request.getRequestURI();
		url = url.substring(url.lastIndexOf("/")+1);
		url = url.replace(".do", "");
		fillHeaderAndFooter(request);
		if(StartUpListener.NAVBARMAP.get(url)!=null){
			request.setAttribute("nav", StartUpListener.NAVBARMAP.get(url).getId());
			request.setAttribute("page", request.getParameter("page"));
		}else{
			request.setAttribute("nav", "none");
		}
		return "front/list";
	}
	
	@RequestMapping(value="/tag/*",method=RequestMethod.GET)
	public String tag(HttpServletRequest request){
		String url = request.getRequestURI();
		url = url.substring(url.lastIndexOf("/")+1);
		url = url.replace(".do", "");
		fillHeaderAndFooter(request);
		if(StartUpListener.TAGMAP.get(url)!=null){
			request.setAttribute("tag", StartUpListener.TAGMAP.get(url).getId());
			request.setAttribute("page", request.getParameter("page"));
		}else{
			request.setAttribute("tag", "none");
		}
		return "front/tags";
	}
	
	@RequestMapping(value="/all")
	public void getArticles(HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> retMap = new LinkedHashMap<String, Object>();
		for (String navbar: StartUpListener.NAVBARMAP.keySet()) {
			if(StartUpListener.NAVBARMAP.get(navbar).getShowOnIndex()==1){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("navbar", navbar);
				map.put("draft", "2");
				map.put("start", 0);
				map.put("pageSize", 8);
				List<Article> list = articleService.getIndexArticle(map);
				if(list!=null&&list.size()>0){
					retMap.put(StartUpListener.NAVBARMAP.get(navbar).getId(), list);
				}
			}
		}
		retMap.put("nav", StartUpListener.NAVBARMAP);
		retMap.put("tag", StartUpListener.TAGMAP);
		PrintUtils.printMsg(response, retMap);
	}
	
	@RequestMapping(value="/get",method=RequestMethod.GET)
	public void getNavArticles(HttpServletRequest request,HttpServletResponse response){
		String navbar = request.getParameter("nav");
		Map<String, Object> retMap = new HashMap<String, Object>();
		if(StringUtils.isEmpty(navbar)){
			PrintUtils.printMsg(response, retMap);
			return;
		}
		String pageSize = request.getParameter("pageSize");
		String pageStart = request.getParameter("pageStart");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("navbar", navbar);
		map.put("draft", "2");
		if(!StringUtils.isEmpty(pageSize)){
			map.put("pageSize", Integer.parseInt(pageSize));
		}
		if(!StringUtils.isEmpty(pageStart)&&Integer.parseInt(pageStart)>0){
			map.put("start", (Integer.parseInt(pageStart)-1)*Integer.parseInt(pageSize));
		}
		List<Article> list = articleService.getIndexArticle(map);
		if(list!=null&&list.size()>0){
			retMap.put(StartUpListener.NAVBARMAP.get(navbar).getId(), list);
		}
		int count = articleService.getIndexArticleCount(map);
		retMap.put("pageCount", count%Integer.parseInt(pageSize)==0?count/Integer.parseInt(pageSize):(count/Integer.parseInt(pageSize)+1));
		retMap.put("page", pageStart);
		retMap.put("nav", StartUpListener.NAVBARMAP);
		retMap.put("tag", StartUpListener.TAGMAP);
		PrintUtils.printMsg(response, retMap);
	}
	
	@RequestMapping(value="/pic",method=RequestMethod.GET)
	public void getPollingPic(HttpServletRequest request,HttpServletResponse response){
		List<PollingPicture> pictures = pollingPictureService.getIndexPollingPictures(new HashMap<String, Object>());
		PrintUtils.printMsg(response, pictures);
	}
	
	@RequestMapping(value="/rec",method=RequestMethod.GET)
	public void getRecommend(HttpServletRequest request,HttpServletResponse response){
		String navbar = request.getParameter("nv");
		Map<String, Object> retMap = new HashMap<String, Object>();
		Map<String, Object> selMap = new HashMap<String, Object>();
		selMap.put("navbar", navbar);
		selMap.put("start", 0);
		selMap.put("draft", "2");
		selMap.put("pageSize", 6);
		List<Article> sortArticle = articleService.getSortArticleByRead(selMap);
		retMap.put("sort", sortArticle);
		List<Article> newestArticle = articleService.getArticles(selMap);
		retMap.put("newest", newestArticle);
		if(StringUtils.isEmpty(navbar)){
			//首页
			selMap.put("showOnIndex", "1");
		}else{
			selMap.put("recommend", "1");
		}
		List<Article> recommendArticle = articleService.getArticles(selMap);
		retMap.put("recommend", recommendArticle);
		PrintUtils.printMsg(response, retMap);
	}
	
	@RequestMapping(value="/article/*",method=RequestMethod.GET)
	public String article(HttpServletRequest request){
		String url = request.getRequestURI();
		url = url.substring(url.lastIndexOf("/")+1);
		url = url.replace(".do", "");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", url);
		fillHeaderAndFooter(request);
		Article article = articleService.getArticleById(map);
		if(article!=null){
			//更新次数
			Article article2 = new Article();
			article2.setId(url);
			article2.setReadCount(article.getReadCount()+1);
			articleService.updateArticle(article2);
			request.setAttribute("article", article);
			request.setAttribute("tag", StartUpListener.TAGMAP);
			return "front/article";
		}else{
			return "error";
		}
		
	}
	
	@RequestMapping(value="/tags",method=RequestMethod.GET)
	public void getTagArticles(HttpServletRequest request,HttpServletResponse response){
		String tag = request.getParameter("tag");
		Map<String, Object> retMap = new HashMap<String, Object>();
		if(StringUtils.isEmpty(tag)){
			PrintUtils.printMsg(response, retMap);
			return;
		}
		String pageSize = request.getParameter("pageSize");
		String pageStart = request.getParameter("pageStart");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tag", tag);
		map.put("draft", "2");
		if(!StringUtils.isEmpty(pageSize)){
			map.put("pageSize", Integer.parseInt(pageSize));
		}
		if(!StringUtils.isEmpty(pageStart)&&Integer.parseInt(pageStart)>0){
			map.put("start", (Integer.parseInt(pageStart)-1)*Integer.parseInt(pageSize));
		}
		List<Article> list = articleService.getIndexArticle(map);
		if(list!=null&&list.size()>0){
			retMap.put(StartUpListener.TAGMAP.get(tag).getId(), list);
		}
		int count = articleService.getIndexArticleCount(map);
		retMap.put("pageCount", count%Integer.parseInt(pageSize)==0?count/Integer.parseInt(pageSize):(count/Integer.parseInt(pageSize)+1));
		retMap.put("page", pageStart);
		retMap.put("tag", StartUpListener.TAGMAP);
		PrintUtils.printMsg(response, retMap);
	}
	
	@RequestMapping(value="/gettags",method=RequestMethod.GET)
	public void taglist(HttpServletResponse response){
		PrintUtils.printMsg(response, StartUpListener.TAGMAP);
	}
	@RequestMapping(value="/tosearch",method=RequestMethod.GET)
	public String toSearch(HttpServletRequest request){
		String url = request.getRequestURI();
		url = url.substring(url.lastIndexOf("/")+1);
		url = url.replace(".do", "");
		fillHeaderAndFooter(request);
		request.setAttribute("page", request.getParameter("page"));
		String title = request.getParameter("title");
		if(!StringUtils.isEmpty(title)){
			try {
				title = new String(title.getBytes("iso8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		String result = CommonUtils.htmlEncode(title);
		request.setAttribute("title", result);
		return "front/search";
	}
	
	@RequestMapping(value="/search",method=RequestMethod.POST)
	public void searchArticles(HttpServletRequest request,HttpServletResponse response){
		String title = request.getParameter("title");
		Map<String, Object> retMap = new HashMap<String, Object>();
		if(StringUtils.isEmpty(title)){
			PrintUtils.printMsg(response, retMap);
			return;
		}
		String pageSize = request.getParameter("pageSize");
		String pageStart = request.getParameter("pageStart");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", title);
		map.put("draft", "2");
		if(!StringUtils.isEmpty(pageSize)){
			map.put("pageSize", Integer.parseInt(pageSize));
		}
		if(!StringUtils.isEmpty(pageStart)&&Integer.parseInt(pageStart)>0){
			map.put("start", (Integer.parseInt(pageStart)-1)*Integer.parseInt(pageSize));
		}
		List<Article> list = articleService.getIndexArticle(map);
		retMap.put("data", list);
		int count = articleService.getIndexArticleCount(map);
		retMap.put("pageCount", count%Integer.parseInt(pageSize)==0?count/Integer.parseInt(pageSize):(count/Integer.parseInt(pageSize)+1));
		retMap.put("page", pageStart);
		retMap.put("tag", StartUpListener.TAGMAP);
		PrintUtils.printMsg(response, retMap);
	}
	
	public static void fillHeaderAndFooter(HttpServletRequest request){
		request.setAttribute("navbar", StartUpListener.NAVBARMAP);
		request.setAttribute("keyword", StartUpListener.KEYWORD);
		request.setAttribute("friendlink", StartUpListener.FRIENDLINKS);
	}
	
}
